% Martin Würms
% 14.9.10
%
% calc pin = iin * uin of a step-up

clc, clear all;

t = 0:0.1:1; % timevector
pwm = 8; % positive pulsewidth in % 
iinmin = 0.1; 
iinmax = 0.2;
uinmin = 0.1;

iin = iinmin*ones(1,size(t)(2));
uin = uinmin*ones(1,size(t)(2));
pin = ones(1,size(t)(2));
rin = ones(1,size(t)(2));

ainpos = (iinmax-iinmin)/(pwm-1);
ainneg = (iinmax-iinmin)/(10-(pwm-1));

for x=1:pwm
    iin(x) = iin(x) + (ainpos * (x-1));
end;
iin(8) = 0.2;
iin(9) = 0.2;
iin(10) = 0.2;
for x = pwm:10
    iin(x) = iin(x) - (ainneg * (x-pwm));
end;

aunpos = (0.05 - 0.1)/(pwm-1);
aunneg = (0.4-0.3)/(10-(pwm-1));
for x=1:pwm
    uin(x) = uin(x) + (aunpos * (x-1));
end;
uin(8) = 0.3;
uin(9) = 0.3;
uin(10) = 0.3;
for x = pwm:10
    uin(x) = uin(x) - (aunneg * (x-pwm));
end;

pin = uin .* iin;
rin = uin ./ iin;

pinmean = sum(pin)/11
rinmean = sum(rin)/11

% plots
figure(1)
subplot(2,1,1)
plot(t,iin);
axis([0,1, 0, 0.3]);
grid on;
xlabel('time t in s');
ylabel('current iin in A');

subplot(2,1,2)
plot(t,uin);
axis([0,1, 0, 0.5]);
grid on;
xlabel('time t in s');
ylabel('voltage uin in V');

figure(2)
plot(t,pin);
%axis([0,1, 0, 0.3]);
grid on;
xlabel('time t in s');
ylabel('power pin');

figure(3)
plot(t,rin);
%axis([0,1, 0, 0.3]);
grid on;
xlabel('time t in s');
ylabel('Resistance rin');

